<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - interactive cubes</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #f0f0f0;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<script src="js/libs/jquery-1.6.4.js"></script>
		<script src="js/libs/three.js"></script>
		<script src="js/loaders/OBJLoader.js"></script>
		<script src="js/controls/TrackballControls.js"></script>
		
		<script>
			var camera, scene, renderer;
			var controls;
			var mesh;
			var mouse = new THREE.Vector2();
			var projector = new THREE.Projector();
			var raycaster = new THREE.Raycaster();

			var INTERSECTED, SELECTED;

			var boxMesh;
			var planeMesh;
			
			var mouseDownX;
			var mouseDownY;
			
			var offset = new THREE.Vector3();
			
			init();
			animate();

			function init() {
				renderer = new THREE.WebGLRenderer();
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.z = 400;

				scene = new THREE.Scene();

				controls = new THREE.TrackballControls( camera );
				controls.rotateSpeed = 1.0;
				controls.zoomSpeed = 1.2;
				controls.panSpeed = 0.8;
				controls.noZoom = false;
				controls.noPan = false;
				controls.staticMoving = true;
				controls.dynamicDampingFactor = 0.3;
				
				var panel = new THREE.CubeGeometry( 300,300,5,2,2,2 );
				var panelMat = new THREE.MeshBasicMaterial( { color: 0xcccccc} );
				planeMesh = new THREE.Mesh( panel, panelMat );
				planeMesh.rotation.setX(90);
				scene.add( planeMesh );

				var box = new THREE.CubeGeometry(50,50,50,8,8,8);
				var boxMat = new THREE.MeshBasicMaterial({color: 0xdd00dd});
				boxMesh = new THREE.Mesh(box, boxMat);
				boxMesh.rotation.setX(90);
				scene.add(boxMesh);

				window.addEventListener( 'resize', onWindowResize, false );
				
				renderer.domElement.addEventListener( 'mousemove', onDocumentMouseMove, false );
				renderer.domElement.addEventListener( 'mousedown', onDocumentMouseDown, false );
				renderer.domElement.addEventListener( 'mouseup', onDocumentMouseUp, false );
			}
			
			function onDocumentMouseMove( event ) {
				event.preventDefault();

				mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
				mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

				var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
				projector.unprojectVector( vector, camera );

				var raycaster = new THREE.Raycaster( camera.position, 
					vector.sub( camera.position ).normalize() );

				if ( SELECTED ) {
					var intersects = raycaster.intersectObject( planeMesh );
					if(intersects.length != 0){
						SELECTED.position.copy( intersects[ 0 ].point.sub( offset ) );
					}
					return;
				}
			}

			function onDocumentMouseDown( event ) {

				event.preventDefault();

				var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
				projector.unprojectVector( vector, camera );

				var raycaster = new THREE.Raycaster( camera.position, 
					vector.sub( camera.position ).normalize() );

				var intersects = raycaster.intersectObjects( [boxMesh] );

				if ( intersects.length > 0 ) {
					controls.enabled = false;
					SELECTED = intersects[ 0 ].object;
				}
			}

			function onDocumentMouseUp( event ) {
				controls.enabled = true;
				SELECTED = null;
			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {
				requestAnimationFrame( animate );
				renderer.render( scene, camera );
				controls.update();
			}
			

		</script>
	</body>
</html>
